//
//  CZVistorView.swift
//  swiftweibo
//
//  Created by Apple on 15/11/23.
//  Copyright © 2015年 tzw. All rights reserved.
//

import UIKit

// 定义协议
protocol CZVistorViewDelegate :NSObjectProtocol{
    // 默认登录必须实现
    func vistorLoginClick()
    func vistorRegisterClick()
}
class CZVistorView: UIView {
    // MARK: - 设置代理属性
    weak var delegate: CZVistorViewDelegate?
    
    // swift中默认所有的view都是通过storyboard/xib来创建的
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置访客的内容
    func setupVistorView(imageName: String,message: String){
       iconView.image = UIImage(named: imageName)
        // 设置文字 
        messageLabel.text = message
        messageLabel.sizeToFit()
        // 隐藏小房子
        homeView.hidden = true
        coverView.hidden = true
    }
    // 准备UI
    func prepareUI(){
        // 添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 给子控件添加约束先关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 给子控件添加约束
        // 转轮X
       
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // Y
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40));
        
        // 小房子x
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 消息x
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // y
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        // 注册按钮x
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // y
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        // 高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录x
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // y
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        // 高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 添加遮盖的约束
        // 左
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 上
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //右
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 下
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    }
    // MARK: - 设置核心动画
    func rotationAnimation(){
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        // 设置旋转的角度
        rotation.toValue = 2 * M_PI
        rotation.repeatCount = MAXFLOAT
        // 当切换控制器的时候.系统默认动画完成了，就会移除动画
        rotation.removedOnCompletion = false
        // 设置时间
        rotation.duration = 20
        // 开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation")
    }
    // MARK: - 核心动画开始和暂停
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    // MARK - 监听按钮的点击事件
    // 登录
    func loginClick(){
//        print("登录")
        // 通知代理
        delegate?.vistorLoginClick()
    }
    // 注册
    func registerClick(){
//        print("注册")
        delegate?.vistorRegisterClick()
    }
    // MARK - 懒加载
    // 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        // 设置文字
        label.text = "关注一些人，看看有什么惊喜关注一些人"
        // 设置文字自动调整
        label.sizeToFit()
        // 设置文字的行数
        label.numberOfLines = 0
        
        return label
        }()
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.sizeToFit()
        // 设置文本的颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setTitle("登录 ", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        // 设置字体
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.sizeToFit()
        // 监听登录的按钮
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    // 添加遮盖视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
